package com.chanyachting.emall.action.register;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.HibernateException;

import com.chanyachting.emall.domain.Member;
import com.chanyachting.emall.exception.AccountNotFoundException;
import com.chanyachting.emall.exception.PasswordErrorException;
import com.chanyachting.emall.form.MemberLoginForm;
import com.chanyachting.emall.service.MemberService;
import com.chanyachting.emall.serviceImpl.MemberServiceImpl;
import com.chanyachting.emall.util.RequestUtil;

public class MemberLoginAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RequestUtil ru=new RequestUtil(request);
		
		MemberService mService = new MemberServiceImpl();
		MemberLoginForm naForm=(MemberLoginForm)form;
		
		Member m = new Member();
		
		String password = naForm.getPassword();
		String account = naForm.getAccount();
		String type = naForm.getType();
		
		m.setPassword(password);
		m.setAccount(account);

		Member tmpMember = null;
		try {
			tmpMember = mService.login(m);
		} catch (AccountNotFoundException e) {
			ru.setMsg("用户名不存在！");
			return mapping.findForward("error");
		} catch (PasswordErrorException e) {
			ru.setMsg("密码不存在！");
			return mapping.findForward("error");
		} catch (HibernateException e) {
			ru.setMsg("DB链接出现错误！");
			return mapping.findForward("error");
		} catch (Exception e) {
			ru.setMsg("出现未知错误，请联系管理员！");
			return mapping.findForward("error");
		}
		
		if (tmpMember == null) {
			return mapping.findForward("error");
		}
		
		if ("2".equals(type) && mService.ifCompanyMember(tmpMember)) {
			;
		} else if ("1".equals(type)) {
			;
		} else {
			return mapping.findForward("error");
		}
		return mapping.findForward("loginSuccess");
	}
}
